package com.wkq.jdbctemplate.commons;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.JdbcUtils;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class RecordRowMapper<T extends Record> implements RowMapper<Record> {
    /**
     * 结果集转换
     *
     * @param resultSet
     * @param i
     * @return
     * @throws SQLException
     */
    @Override
    public Record mapRow(ResultSet resultSet, int i) throws SQLException {
        Record record = new Record();

        ResultSetMetaData metaData = resultSet.getMetaData();
        for (int col = 0; col < metaData.getColumnCount(); col++) {
            record.put(CamelCaseUtils.toCamelCase(JdbcUtils.lookupColumnName(metaData, col + 1).replaceAll(" ", "").toLowerCase()), resultSet.getObject(col + 1));
        }
        return record;
    }

}
